<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="98px">
      <!-- <el-form-item label="系统名称" prop="systemName">
        <el-input
          v-model="queryParams.systemName"
          placeholder="请输入系统名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="员工编号" prop="userNo">
        <el-input
          v-model="queryParams.userNo"
          placeholder="请输入员工编号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入员工姓名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="员工账号" prop="loginName">
        <el-input
          v-model="queryParams.loginName"
          placeholder="请输入员工账号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="主管单位" prop="competentDept">
        <el-input
          v-model="queryParams.competentDept"
          placeholder="请输入主管单位"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主管部门" prop="competentOrg">
        <el-input
          v-model="queryParams.competentOrg"
          placeholder="请输入主管部门"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="最后登录时间" prop="loginDate">
        <el-date-picker clearable size="small" style="width: 240px"
          v-model="queryParams.loginDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择最后登录时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="帐号状态" prop="userStatus">
        <el-select v-model="queryParams.userStatus" placeholder="请选择帐号状态" clearable size="small" style="width: 240px">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="允许标志" prop="authFlag">
        <el-select v-model="queryParams.authFlag" placeholder="请选择允许标志" clearable size="small" style="width: 240px">
          <el-option
              v-for="dict in authFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="更新时间" prop="sysUpdateTime">
        <el-date-picker clearable size="small" style="width: 240px"
          v-model="queryParams.sysUpdateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择更新时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="账号代持人" prop="holderNames">
        <el-input
          v-model="queryParams.holderNames"
          placeholder="请输入账号代持人"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
       <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-refresh"
          size="mini"
          @click="syncBpm"
        >同步BPM状态</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:bpm:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:bpm:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:bpm:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:bpm:export']"
        >导出</el-button>
      </el-col> -->
	  <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="bpmList" :default-sort="{ prop: 'holderNames', order: 'descending' }" @sort-change="tableSortChange">
      <!--<el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" prop="id" />-->
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="系统名称" align="center" prop="systemName" :sort-orders="['ascending', 'descending']" sortable="custom" v-if="false"/>
      <el-table-column label="员工编号" align="center" prop="userNo" :sort-orders="['ascending', 'descending']" sortable="custom"/>
      <el-table-column label="员工姓名" align="center" prop="userName" :sort-orders="['ascending', 'descending']" sortable="custom"/>
      <el-table-column label="员工账号" align="center" prop="loginName" :sort-orders="['ascending', 'descending']" sortable="custom" v-if="false"/>
      <el-table-column label="主管单位" align="center" prop="competentDept" :sort-orders="['ascending', 'descending']" sortable="custom"/>
      <el-table-column label="主管部门" align="center" prop="competentOrg" :sort-orders="['ascending', 'descending']" sortable="custom"/>
      <el-table-column label="帐号状态" align="center" prop="userStatus" :formatter="statusFormat" :sort-orders="['ascending', 'descending']" sortable="custom"/>
      <el-table-column label="允许标志" align="center" prop="authFlag" :sort-orders="['ascending', 'descending']" sortable="custom">
        <template slot-scope="scope">
          <el-switch
            v-if="scope.row.systemName"
            v-model="scope.row.authFlag"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
       <!-- <el-table-column label="最后登录时间" align="center" prop="loginDate" width="180" :sort-orders="['ascending', 'descending']" sortable="custom">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="更新时间" align="center" prop="sysUpdateTime" width="180" :sort-orders="['ascending', 'descending']" sortable="custom">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sysUpdateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="账号代持人" align="center" prop="holderNames" :sort-orders="['ascending', 'descending']" sortable="custom"/>
      <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:bpm:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:bpm:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改bpm用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="系统名称" prop="systemName">
          <el-input v-model="form.systemName" placeholder="请输入系统名称" />
        </el-form-item>
        <el-form-item label="员工编号" prop="userNo">
          <el-input v-model="form.userNo" placeholder="请输入员工编号" />
        </el-form-item>
        <el-form-item label="员工账号" prop="loginName">
          <el-input v-model="form.loginName" placeholder="请输入员工账号" />
        </el-form-item>
        <el-form-item label="主管单位" prop="competentDept">
          <el-input v-model="form.competentDept" placeholder="请输入主管单位" />
        </el-form-item>
        <el-form-item label="主管部门" prop="competentOrg">
          <el-input v-model="form.competentOrg" placeholder="请输入主管部门" />
        </el-form-item>
        <!-- <el-form-item label="最后登录时间" prop="loginDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.loginDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择最后登录时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="帐号状态">
          <el-radio-group v-model="form.userStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="允许标志" prop="authFlag">
          <el-input v-model="form.authFlag" placeholder="请输入允许标志" />
        </el-form-item>
        <!-- <el-form-item label="更新时间" prop="sysUpdateTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.sysUpdateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择更新时间">
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBpm, getBpm, delBpm, addBpm, updateBpm, exportBpm,syncBpm } from "@/api/system/bpm";

export default {
  name: "Bpm",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // bpm用户信息表格数据
      bpmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        systemName: null,
        userNo: null,
        loginName: null,
        competentDept: null,
        competentOrg: null,
        loginDate: null,
        userStatus: null,
        authFlag: null,
        sysUpdateTime: null,
        holderNames:null
      },
      // 表单参数
      form: {},
      // 状态数据字典
      statusOptions: [],
       // 流程类别数据字典
      authFlagOptions: [],
      // 表单校验
      rules: {
        systemName: [
          { required: true, message: "系统名称不能为空", trigger: "blur" }
        ],
        userNo: [
          { required: true, message: "员工编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("outside_user_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_auth_flag").then(response => {
      this.authFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询bpm用户信息列表 */
    getList() {
      this.loading = true;
      listBpm(this.queryParams).then(response => {
        this.bpmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.userStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        systemName: null,
        userNo: null,
        userName: null,
        loginName: null,
        competentDept: null,
        competentOrg: null,
        loginDate: null,
        userStatus: "0",
        delFlag: null,
        authFlag: null,
        sysUpdateTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加bpm用户信息";
    },
    syncBpm() {
        this.$confirm('是否开始同步BPM用户状态', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return syncBpm();
        }).then(() => {
          this.getList();
          this.msgSuccess("同步成功");
        })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBpm(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改bpm用户信息";
      });
    },
    // 系统状态修改
    handleStatusChange(row) {
      let text = row.authFlag === "0" ? "允许" : "不允许";
      const data = {
        id: row.id,
        authFlag: row.authFlag
      };
      this.$confirm(
        '确认要"' + text + '""' + row.userNo + '"吗?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
           updateBpm(data);
        })
        .then(() => {
          this.msgSuccess("修改成功");
          this.getList();
        })
        .catch(function () {
          row.authFlag = row.authFlag === "0" ? "1" : "0";
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBpm(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBpm(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除bpm用户信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBpm(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有bpm用户信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportBpm(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    tableSortChange(column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    },
  }
};
</script>
